package cn.itcast.controller;

import cn.itcast.domain.Standard;
import cn.itcast.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-01月-20日  16:00
 * Look at the wine in the glass toast the rich first.
 */

@Controller
public class StandardController {

    @Autowired
    private StandardService standardService;


    @RequestMapping("/standard_save")
    public String standard_save(Standard standard){
        standardService.save(standard);
        return "redirect:/pages/base/standard.html";
    }

    @RequestMapping("/standard_pageQuery")
    @ResponseBody
    public Map pageQuery(int page ,int rows){
        Page<Standard> pageResult =  standardService.pageQuery(page,rows);

        Map<String,Object> map = new HashMap<>();
        map.put("total",pageResult.getTotalElements());
        map.put("rows",pageResult.getContent());

        //System.out.println("total = " + map.get("total"));
        //System.out.println("rows = " + map.get("rows"));
        return map;
    }

    @RequestMapping("/deleById")
    public String deleById(String id){
        System.out.print("id是:"+id);
        standardService.deleById(id);
        return "redirect:/pages/base/standard.html";
    }

    /**
     * 前台需要[{},{}]格式的数据,可以返回一个list集合里面都是键值对
     * @return
     */
    @RequestMapping("/standard_findAll")
    @ResponseBody
    public List<Standard> standard_findAll() {
        List<Standard> standardList = standardService.standard_findAll();
        return standardList;
    }

/*
    public List<Map<String,Object>> standard_findAll(){
        List<Standard> standardList = standardService.standard_findAll();
        List<Map<String,Object>> mapList = new ArrayList<>();
        Map<String,Object> map = null;
        for (Standard standard : standardList) {
            map = new HashMap<>();
            String id = standard.getId().toString();
            String name = standard.getName();
            map.put("id",id);
            map.put("name",name);
            mapList.add(map);
        }
        return  mapList;
    }
*/
}
